//
//  CollectionViewController.m
//  LiTingFeng
//
//  Created by xinxuan on 16/3/8.
//  Copyright © 2016年 Bullgo. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionTableViewCell.h"
#import "CostomManager.h"
#import "CostomerModel.h"
#import "CustomViewController.h"
@interface CollectionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *modelsArray;
@end

@implementation CollectionViewController

static NSString *ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"课程收藏";
    self.navigationItem.rightBarButtonItem.title = @"编辑";
    [self getModelWithTitle];
    
    [self.view addSubview:self.tableView];
    
}


#pragma mark ---- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 40)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        [_tableView registerNib:[UINib nibWithNibName:@"CollectionTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    }
    return _tableView;
}

-(void)getModelWithTitle{
    _modelsArray = [NSMutableArray array];
    
    _modelsArray = [[CostomManager shareManager]fetchAllData];
    
    
}











-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105;
    
}






-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld",_modelsArray.count);
    return _modelsArray.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    CostomerModel * costomMode = _modelsArray[indexPath.row];
    cell.liLiaoLabel.text = [NSString stringWithFormat:@"%ld",costomMode.liLiaoSliderValue];
    cell.reFuLabel.text = [NSString stringWithFormat:@"%ld",costomMode.reFuSliderValue];
    cell.anMoLabel.text = [NSString stringWithFormat:@"%ld",costomMode.anMoSliderValue];
    cell.NameLabel.text = costomMode.name;

    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CostomerModel * model = [CostomerModel new];
    model = _modelsArray[indexPath.row];
    
    CustomViewController * customVc = [[CustomViewController alloc] init];
    customVc.model = model;
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"已收藏"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [self.navigationController pushViewController:customVc animated:YES];
    
    
}



#pragma mark --------TableView 编辑-----------

//第一步,使处于可编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
    
    self.navigationItem.rightBarButtonItem.title = editing?@"完成":@"删除";
    
}


//第二步,指定那些路径可以被编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
    
}





//第三步,指定编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//第四步,完成编辑

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        CommenModel * model = _modelsArray[indexPath.row];
        
        [[CostomManager shareManager]deleteData:model];
        
        [_modelsArray removeObject:model];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
        
        [self.tableView reloadData];
        
        
    }
}




@end
